import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.*;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 


/**
 * 
 * Handles all functions necessary for acquiring training data
 * 
 * @author ta4514
 *
 */
public class training {

	//Hash table containing categories
	private HashMap<String,Integer> cats;
	
	public training()
	{
		cats = new HashMap<String,Integer>();
	}
	
	public static void main(String[] args)
	{
		training t = new training();
		t.readAndPrint();
	}
	
	/**
	 * Adds a cat to cats or increment counter if already there
	 */
	public void addCat(String cat)
	{
		int val;
		
		if(cats.containsKey(cat))
		{
			val = cats.get(cat) + 1;
			cats.put(cat,val);
		}
		else
		{
			cats.put(cat,1);
		}
	}
	
	/**
	 * Prints out contents of cat hashmap
	 */
	public void printCat()
	{
		Iterator iter = cats.entrySet().iterator();
		while(iter.hasNext()){
			Map.Entry<String,Integer> next = (Map.Entry<String,Integer>) iter.next();
			System.out.println("Category is "+ next.getKey() +": "+next.getValue());
		}
	}
	/**
	 * Reads through training data and prints out all 
	 * 
	 */
	public void readAndPrint()
	{
		String cat;
		
		   try {

	            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	            Document doc = docBuilder.parse (new File("/home/user-pool/users/ta4514/news.xml"));

	            //doc.getDocumentElement().normalize();


	            NodeList arts = doc.getElementsByTagName("news");
	            int totalPersons = arts.getLength();
	            System.out.println("Total no of people : " + totalPersons);

	            for(int s=0; s<arts.getLength() ; s++){


	                Node firstPersonNode = arts.item(s);
	                if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


	                    Element firstPersonElement = (Element)firstPersonNode;

	                    //-------
	                    NodeList firstNameList = firstPersonElement.getElementsByTagName("category");
	                    Element firstNameElement = (Element)firstNameList.item(0);

	                    NodeList textFNList = firstNameElement.getChildNodes();
	                    cat = textFNList.item(0).getNodeValue().trim();
	                    addCat(cat);

	                }//end of if clause


	            }//end of for loop with s var
	            printCat();

	        }catch (SAXParseException err) {
	        System.out.println ("** Parsing error" + ", line " 
	             + err.getLineNumber () + ", uri " + err.getSystemId ());
	        System.out.println(" " + err.getMessage ());

	        }catch (SAXException e) {
	        Exception x = e.getException ();
	        ((x == null) ? e : x).printStackTrace ();

	        }catch (Throwable t) {
	        t.printStackTrace ();
	        }

	}
}
